package com.itheima.aspect;


import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

/**
 * 统计接口耗时切面
 */
//@Aspect
@Component
@Slf4j
public class CountTimeAspect {
    @Resource
    HttpServletRequest request;

    @Around("execution( * com.itheima.service.*.*.*(..))")
    public Object count(ProceedingJoinPoint point){
        long start = System.currentTimeMillis();
        //获取参数名
        log.info("参数为{}",point.getArgs());
        //获取方法类名
        log.info("方法类名为{}",point.getTarget());
        String token = request.getHeader("token");
        log.info("token为{}",token);
        //执行父类方法
        try {
            Object proceed = point.proceed();
            //获取返回值
            log.info("返回值为{}",proceed);

            long end =System.currentTimeMillis() - start;
            log.info("{} 执行时间为：{} ms",point.getSignature().getName(),end);
            return proceed;
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
    }

}
